package com.nash.demands;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class DocItemsActivity extends Activity {
  public ListView list;
  private Cursor data;
  private DocTabActivity parentActivity;

  public void onCreate(Bundle savedInstanceState) {
    try {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.doc_items);
      parentActivity = ((DocTabActivity) this.getParent());
      parentActivity.docItemsActivity = this;

      list = (ListView) findViewById(R.id.listViewDocItems);
      list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
          AmountDialog ad=new AmountDialog();
          long id_tu=((Cursor)arg0.getAdapter().getItem(arg2)).getLong(3);
          //ad.editAmount(DocItemsActivity.this, id_tu);
        }
      });
      
      setAdapterDocItems();
    } catch (Exception e) {
      Toast.makeText(this.getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
    }
  }

  public void setAdapterDocItems() {
    final String itemsFields[] = { Helper.TBL_TU_NAME, DocTabActivity.TBL_TEMP_ITEM_AMOUNT, BaseColumns._ID };
    final int[] itemsTo = new int[] { R.id.item_name_entry, R.id.item_amount_entry };
    String sql = "select tu." + Helper.TBL_TU_NAME + ", t." + DocTabActivity.TBL_TEMP_ITEM_AMOUNT + ", t._id, tu._id as id_tu from " + DocTabActivity.TBL_TEMP_ITEM + " t," + Helper.TBL_TU + " tu where tu." + BaseColumns._ID + "=t."
        + DocTabActivity.TBL_TEMP_ITEM_ID_TU + " order by tu.name";
    data = DemandsActivity.db.rawQuery(sql, null);
    startManagingCursor(data);
    CursorAdapter dataSource = new SimpleCursorAdapter(this, R.layout.item_entry, data, itemsFields, itemsTo);
    list.setAdapter(dataSource);
  }

  @Override
  public void onBackPressed() {
    ((DocTabActivity)this.getParent()).myOnBackPressed();
  }
  
}